// pages/craftsman_page/craftsman_page.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import { getCategoryList, getStaffWorkList, getMemberInfo, getWorkDetail, deletaGoodsItem, getStaffDetail, waitComfirmOrders, staffFinishService } from '../../static/api/api.js'
let app = new getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isShowSiderBar:false,
        isShowDetail:false,
        memberInfo:null,
        isManger:false,
        status:'',
        pages: {
            curPage: 1,
            totalPage: 1
        },
        isShowTip:false,
        isNoMore: false,
        pageLoad: false,
        orderList:null,
        isShowDetail:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        this.setData({
            memberInfo:app.globalData.memberInfo
        })
        // app.getUserInfo(function (data) {
        //     console.log('用户信息');
        // })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this._getUserInfo()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    showSiderBar() {
        this.setData({
            isShowSiderBar: true
        })
    },
    hideSiderBar() {
        this.setData({
            isShowSiderBar: false
        })
    },
    showAddOrderr() {
        this.setData({
            isShowDetail: true
        })
    },
    hideAddOrderr() {
        this.setData({
            isShowDetail: false
        })
    },
    lower(){
        this.getStaffOrderList()
    },
    goUrl(e) {
        utils.goNavigationUrl(e)
    },
    _getUserInfo(cb) {
        let that = this
        wx.showLoading({
            title: '加载中...',
        })
        getMemberInfo().then(response => {
            console.log('请求成功')
            wx.hideLoading();
            this.setData({
                memberInfo:response.data
            })
            this.getStaffOrderList()
        }).catch(response => {
            wx.hideLoading();
            console.log(response)
        })
    },
    //获取员工订单
    getStaffOrderList() {
        let that = this;
        if (this.data.pageLoad) return;
        if (this.data.pages.curPage > this.data.pages.totalPage) return;
        this.setData({
            pageLoad: true
        })
        let params = {
            order_date: this.data.status
        }
        waitComfirmOrders(params).then(response => {

            let data = response.data;
            this.setData({
                'confirm': response.confirm || ''
            })
            if (that.data.pages.curPage == 1) {
                that.setData({
                    orderList: data.lists
                })
            } else {
                that.setData({
                    orderList: that.data.orderList.concat(data.lists)
                })
            }
            if(this.data.orderList.length>0){
                this.setData({
                    'isShowTip':true
                })
            }
            that.setData({
                'pages.curPage': that.data.pages.curPage + 1,
                'pages.totalPage': data.pages.total_page,
                'pageLoad': false
            })
            if (this.data.pages.curPage >= this.data.pages.totalPage) {
                this.setData({
                    isNoMore: true
                })
            };
        }).catch(response => {
            this.setData({
                'pageLoad': false
            })
        })
    },
    inputValueChange(e) {
        utils.inputValueChange(e, this)
    },
    comfirmServiceOver(e) {
        let dataset = e.currentTarget.dataset;
        let that = this;
        wx.showModal({
            title: '提示',
            content: '请确定当前服务是否完成',
            success(res) {
                if (res.confirm) {
                    that.setData({
                        'curItem': dataset.item,
                        'curIndex': dataset.index
                    })
                    that.showAddOrderr(e)
                } else if (res.cancel) {

                }
            }
        })

    },
    showAddOrderr(e) {
        let dataset = e.currentTarget.dataset;
        this.setData({
            'isShowDetail': true,
            'curItem':dataset.item,
            'curIndex':dataset.index
        })
    },
    //员工确认服务是否完成
    staffFinishService(e) {
        let dataset = e.currentTarget.dataset;
        let that = this;
        let params = {
            order_id: dataset.item.id,
            is_seconds: dataset.is_seconds == 1 ? false : true,
            seconds_price: this.data.seconds_price,
            seconds_remark: this.data.seconds_remark
        }
        if (dataset.is_seconds == 2) {
            if (this.data.seconds_price == '') {
                wx.showToast({
                    icon: 'none',
                    title: '请输入加单金额',
                })
                return;
            }
            if (this.data.seconds_remark == '') {
                wx.showToast({
                    icon: 'none',
                    title: '请输入加单备注',
                })
                return;
            }
        }
        wx.showLoading({
            title: '加载中...',
        })
        staffFinishService(params).then(response => {
            wx.hideLoading()
            wx.showToast({
                icon: 'none',
                title: '操作成功，请等待用户确认',
            })
            that.data.orderList.splice(that.data.curIndex, 1)
            that.setData({
                'orderList': that.data.orderList,
                'isShowDetail': false
            })
        }).catch(response => {
            wx.hideLoading()
        })
    },
    changeStatus(e) {
        let dataset = e.currentTarget.dataset;

        this.setData({
            'status': dataset.status,
            'pages.curPage': 1,
            'pages.totalPage': 1,
            'pageLoad': false,
            'orderList': null
        })
        this.getStaffOrderList()
    },
})